home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 …SCII & the Runetime Code / ADC Developer CD (1992-07) (''Butch ASCII And The Runtime Code'')_iso / Dev.CD 199207.iso / Tools & Apps / OS⁄Toolbox / Apple Events / AE Word Services 1.0d6 / Writeswell Jr. Source / GenHandlers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-23  |  3.5 KB  |  114 lines  |  [TEXT/KAHL]

  1. /* GenHandlers.c
  2.  * Do the generic event handling according to Richard Clarke from Dev U.
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 18 Dec 91 Mike Crawford
  14.  */
  15.  
  16. #include <AppleEvents.h>
  17. #include <AEObjects.h>
  18. #include <AERegistry.h>
  19. #include "TBConstants.h"
  20. #include "TBGlobals.h"
  21. #include "GenHandlers.h"
  22. #include "ObNull.h"
  23. #include "ObWind.h"
  24. #include "ObText.h"
  25. #include "Gripe.h"
  26.  
  27. OSErr InitGenericHandlers( void )
  28. {
  29.     OSErr err;
  30.  
  31.     if ( err = AEInstallEventHandler( kAECoreSuite,
  32.                                 typeWildCard,
  33.                                 (EventHandlerProcPtr)GenericHandler,
  34.                                 0,
  35.                                 false ) ){
  36.         Gripe( "\poapp install failed" );
  37.         return err;
  38.     }
  39.     return err;
  40. }
  41.  
  42. /* In the generic handler, we extract the direct object, resolve it, and identify
  43.  * the type of the finally resolved object (window, text, etc.).  Then we call a routine
  44.  * that handles events for that kind of object.
  45.  *
  46.  * Note that this essentially circumvents the AppleEvent handler dispatch table, and
  47.  * replaces it with our own.  The reason is that it makes it possible to have a separate
  48.  * source file for each kind of data object that will not need to be changed if other
  49.  * sorts of objects are added.  If we have separate event handlers, then when we add
  50.  * an object, we will need to add the code to handle that object to each event handler.
  51.  * In this scheme, if we add support for a new kind of object, we add a source file
  52.  * full of handlers for that object, and add an entry (and a new #include line) in this
  53.  * file.
  54.  *
  55.  * This should make the code more maintainable.  It would be real natural to use an
  56.  * object-oriented language here, where each object would have a standard set of methods
  57.  * for each event that all objects are supposed to handle.
  58.  */
  59.  
  60. pascal OSErr GenericHandler( AppleEvent *theAppleEventPtr,
  61.                                 AppleEvent *replyEventPtr,
  62.                                 long refCon )
  63. {
  64.     AEDesc paramDesc;
  65.     AEDesc tokenDesc;
  66.     OSErr err;
  67.  
  68.     err = AEGetParamDesc( theAppleEventPtr,
  69.                             keyDirectObject,
  70.                             typeObjectSpecifier,
  71.                             ¶mDesc );
  72.  
  73.     if ( err && err != errAEDescNotFound ){        /* It's OK for no parameter to be there */
  74.         Gripe( "\pAEGetParamDesc failed" );
  75.         return err;
  76.     }
  77.     
  78.     if ( err != errAEDescNotFound ){
  79.         err = AEResolve( ¶mDesc,
  80.                             kAEIDoMinimum,
  81.                             &tokenDesc );
  82.         if ( err ){
  83.             Gripe( "\pAEResolve failed" );
  84.             return err;
  85.         }
  86.     }else{
  87.         /* Copy the null param to the token desc */
  88.         err = AEDuplicateDesc( ¶mDesc, &tokenDesc );
  89.         if ( err ){
  90.             Gripe( "\pAEDuplicateDesc failed" );
  91.             return err;
  92.         }
  93.     }
  94.     
  95.     switch ( tokenDesc.descriptorType ){
  96.     
  97.         case typeNull:
  98.             err = DispatchNull( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  99.             break;
  100.         case cWindow:
  101.             err = DispatchWind( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  102.             break;
  103.         case typeTEText:
  104.             err = DispatchTEText( &tokenDesc, theAppleEventPtr, replyEventPtr, refCon );
  105.             break;
  106.         default:
  107.             Gripe( "\pUnknown object type" );
  108.             return errAEEventNotHandled;
  109.         
  110.     }
  111.                             
  112.     return noErr;
  113. }
  114.